package com.example.yijian.edit.view.popup;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.yijian.R;
import com.example.yijian.edit.adapter.CurveSpeedViewAdapter;
import com.example.yijian.edit.clipEdit.CurveSpeedUtil;
import com.example.yijian.edit.data.ChangeSpeedCurveInfo;
import com.example.yijian.edit.interfaces.OnItemClickListener;
import com.example.yijian.edit.view.EditChangeSpeedCurveView;
import com.example.yijian.utils.datainfo.ClipInfo;
import com.meicam.sdk.NvsVideoClip;

import java.util.List;

public class SpeedCurvePopup extends BaseCommonPopupWindow {
    private RecyclerView rv_curve;
    private ImageView mIvConfirm;
    private List<ChangeSpeedCurveInfo> changeSpeedCurveInfoList;
    private CurveSpeedViewAdapter mAdapter;


    private long clipDuration;

    private String mCurveSpeed;
    private boolean hasCurveSpeed = false;
    private int mCurrentSelectedCurvePosition = 0;
    private OnFunctionListener onFunctionListener;

    public SpeedCurvePopup(Context context) {
        super(context);
        setContentView(R.layout.popup_speed_curve);
        initView();
        initCurveView();
    }

    private void initView() {
        rv_curve = getContentView().findViewById(R.id.rv_curve);
        mIvConfirm = getContentView().findViewById(R.id.speedFinish);
        mIvConfirm.setOnClickListener(v -> dismiss());
    }

    public void setData(ClipInfo clipInfo) {
        clipDuration = clipInfo.getTrimOut() - clipInfo.getTrimIn();
        //获取到当前设置的曲线效果
        ChangeSpeedCurveInfo changeSpeedCurveInfo = clipInfo.getCurveSpeed();
        if (null != changeSpeedCurveInfo) {
            int index = changeSpeedCurveInfo.index;
            mCurrentSelectedCurvePosition = index;
            changeSpeedCurveInfoList.get(index).speed = changeSpeedCurveInfo.speed;
            mAdapter.setSelectedPosition(mCurrentSelectedCurvePosition);
        }
    }

    private void initCurveView() {
        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false);
        rv_curve.setLayoutManager(layoutManager);
        mAdapter = new CurveSpeedViewAdapter(getContext());
        rv_curve.setAdapter(mAdapter);
        changeSpeedCurveInfoList = CurveSpeedUtil.listSpeedFromJson(getContext());
        mAdapter.updateData(changeSpeedCurveInfoList);
        mAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(View view, int pos) {
                hasCurveSpeed = pos != 0;
                //选中一种曲线变速的规则
                //Choose a curve speed rule
                ChangeSpeedCurveInfo changeSpeedCurveInfo = changeSpeedCurveInfoList.get(pos);
                //当前已经是选中的，则去编辑
                //It is currently selected, go to edit
                if (mCurrentSelectedCurvePosition == pos && pos != 0) {
                    onFunctionListener.onEditCurve(changeSpeedCurveInfo);
                    dismiss(true);
//                    //如果 手动滑动过播放进度  进入曲线编辑页面暂停 否则从0播放
//                    //If you manually slide over the playback progress,
//                    // enter the curve editing page to pause, otherwise play from 0
//                    if (mPlayTimestamp == 0) {
//                        playVideo(0);
//                    } else {
//                        //如果设置自定义变速并且没有调节过 则直接使用seek的时间节点更新
//                        //If you set a custom variable speed and have not adjusted it,
//                        // use the seek time node to update directly
//                        long bzPosition = mPlayTimestamp;
//                        if (!TextUtils.isEmpty(mCurveSpeed)) {
//                            bzPosition = getClipPositionFromTimelinePosition(bzPosition);
//                        }
//                        //更新基准线的位置 Update the position of the baseline
//                        changeSpeedCurveView.upDataPlayProgress(bzPosition);
//                        //stopPlayVideo();
//                    }
                } else {
                    //第一次选中 从片段开始位置播放
                    //Select for the first time to play from the beginning of the clip
                    mAdapter.setSelectedPosition(pos);
                    mCurrentSelectedCurvePosition = pos;
                    if (pos != 0) {
                        hasCurveSpeed = true;
                    }
                    mCurveSpeed = changeSpeedCurveInfo.speed;
                    //TODO 设置选中的效果
                    onFunctionListener.onSpeedChanged(changeSpeedCurveInfo);
//                    //选择无暂定到当前位置  否则从头开始播放
//                    //Choose No Tentatively to the current position, otherwise start playing from the beginning
//                    if (pos == 0) {
//                        stopPlayVideo();
//                    } else {
//                        //从0开始播放 Start playing from 0
//                        playVideo(0);
//                        hasCurveSpeed = true;
//                    }

                }
            }
        });
    }

    public void setOnFunctionListener(OnFunctionListener callBack) {
        this.onFunctionListener = callBack;
    }


    public interface OnFunctionListener {
        //        void onConfirm(float speed, boolean changeVoice);
//
        void onEditCurve(ChangeSpeedCurveInfo changeSpeedCurveInfo);

        void onSpeedChanged(ChangeSpeedCurveInfo changeSpeedCurveInfo);
    }

}
